package com.example.module_motorcircle.report

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.lib_common.Router
import com.example.lib_common.base.BaseToolBarActivity
import com.example.lib_common.observeWithLifecycle
import com.example.lib_common.state.UIState
import com.example.lib_common.utils.ToastUtils
import com.example.lib_net.model.api.ReportTypeModel
import com.example.module_motorcircle.R
import com.example.module_motorcircle.databinding.ActivityReportBinding
import com.example.module_motorcircle.report.adapter.ReportTypeAdapter
import com.example.module_motorcircle.report.viewmodel.ReportViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = Router.MOTOR_CIRCLE_REPORT_ACTIVITY)
class ReportActivity : BaseToolBarActivity<ReportViewModel,ActivityReportBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_report
    override val mViewModel: ReportViewModel by viewModels()

    //类型 1:帖子 2:文章
    @Autowired
    lateinit var type:String
    //帖子id
    @Autowired
    lateinit var id:String
    //当前选中的举报类型
    private var mCheckedType:ReportTypeModel? = null
    override fun initData() {
        mViewModel.reportTypeList()
    }

    override fun initListener() {
        //订阅举报列表
        mViewModel.reportTypeListState_.observeWithLifecycle(this){
            when(it){
                is UIState.Success -> {
                    setLoadingViewVisible(false)
                    mViewModel.mReportTypeList[0].isChecked = true
                    mCheckedType = mViewModel.mReportTypeList[0]
                    mBinding.rv.adapter = ReportTypeAdapter(mViewModel.mReportTypeList){model->
                        mCheckedType = model //更新选中的类型
                    }
                }
                is UIState.Error -> {
                    setLoadingViewVisible(false)
                    ToastUtils.showLong(this,"失败$it")
                }
                is UIState.Loading -> {
                    setLoadingViewVisible(true,"数据加载中。。。。")
                }
                is UIState.Empty -> {
                    setLoadingViewVisible(false)
                    ToastUtils.showLong(this,"空数据")
                }
                else -> {}
            }
        }
        //订阅举报帖子
        mViewModel.reportState_.observeWithLifecycle(this){
            when(it){
                is UIState.Success -> {
                    setLoadingViewVisible(false)
                    ToastUtils.showLong(this,"举报成功")
                    finish()
                }
                is UIState.Error -> {
                    setLoadingViewVisible(false)
                    ToastUtils.showLong(this,"举报失败$it")
                }
                is UIState.Loading -> {
                    setLoadingViewVisible(true)
                }
                else->{

                }
            }
        }
        //举报帖子
        mBinding.btnReport.setOnClickListener {
            mViewModel.reportPost(id,mCheckedType?.typeId.toString())
        }
    }

}